﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace EBA.Xaml.Mvvm
{
    public class WindowView : IView
    {
        //private bool? ModalDialogResult = null;
        //private IntPtr OwnerHandle;
        //private IntPtr Handle = IntPtr.Zero;
        public WindowView(Window window)
        {
            this.Window = window;
           
            //if (this.Window.Owner != null)
            //{
            //    OwnerHandle = (new System.Windows.Interop.WindowInteropHelper(this.Window.Owner)).Handle;
            //}
            //else
            //{
            //    this.OwnerHandle = (new System.Windows.Interop.WindowInteropHelper(Application.Current.MainWindow)).Handle;
            //}


            //IntPtr Handle = NativeMethods.GetWindowHandle(this.Window);
            //IntPtr focused = NativeMethods.GetForegroundWindow();
            //if (Handle != focused)
            //{
            //    NativeMethods.EnableWindow(Handle, true);
            //    NativeMethods.SetForegroundWindow(Handle);
            //}
            this.Window.Closing += new System.ComponentModel.CancelEventHandler(Window_Closing);
        }

        public Window Window { get; private set; }

        public Window Owner
        {
            get
            {
                return this.Window == null ? null : this.Window.Owner;
            }
            set
            {
                if (this.Window != null)
                {
                    this.Window.Owner = value;
                }

                //this.OwnerHandle = NativeMethods.GetWindowHandle(this.Window.Owner);
            }
        }



        public string Title
        {
            get { return this.Window.Title; }
        }

        public object DataContext
        {
            get
            {
                return this.Window.DataContext;
            }
            set
            {
                this.Window.DataContext = value;
            }
        }

        public void Show()
        {
            this.Window.Show();
        }


        public void Close()
        {
            this.Window.Close();          
        }


        public bool? ShowDialog()
        {
            return this.Window.ShowDialog();
            //NativeMethods.EnableWindow(OwnerHandle, false);
            //new ShowAndWaitHelper(this.Window).ShowAndWait();
            //return ModalDialogResult;
        }

        void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Window.Closing -= new System.ComponentModel.CancelEventHandler(Window_Closing);
            //NativeMethods.EnableWindow(Handle, false);
            //NativeMethods.EnableWindow(OwnerHandle, true);
        }




        public void Activate()
        {
            this.Window.Activate();
        }
    }
}
